'use strict';

var _react = require('react');

var _react2 = _interopRequireDefault2(_react);

function _interopRequireDefault2(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

var _express = require('express');

var _express2 = _interopRequireDefault(_express);

var _reactRedux = require('react-redux');

var _reactRouter = require('react-router');

var _server = require('react-dom/server');

var _createMemoryHistory = require('history/lib/createMemoryHistory');

var _createMemoryHistory2 = _interopRequireDefault(_createMemoryHistory);

var _getMuiTheme = require('material-ui/styles/getMuiTheme');

var _getMuiTheme2 = _interopRequireDefault(_getMuiTheme);

var _MuiThemeProvider = require('material-ui/styles/MuiThemeProvider');

var _MuiThemeProvider2 = _interopRequireDefault(_MuiThemeProvider);

var _colors = require('material-ui/styles/colors');

var _routes = require('../dist/routes');

var _routes2 = _interopRequireDefault(_routes);

var _indexHTML = require('./indexHTML');

var _indexHTML2 = _interopRequireDefault(_indexHTML);

var _store = require('../dist/redux/store');

var _store2 = _interopRequireDefault(_store);

var ReactDOMServer = require('react-dom/server');

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

// serverRender
var router = _express2.default.Router();

var history = (0, _createMemoryHistory2.default)();
var location = history.getCurrentLocation();

var store = (0, _store2.default)(_reactRouter.browserHistory);

// 主题
var muiTheme = (0, _getMuiTheme2.default)({
  fontFamily: 'Roboto, Microsoft YaHei, Helvetica, Arial, sans-serif',
  palette: {
    primary1Color: _colors.orange500,
    primary2Color: _colors.cyan500,
    primary3Color: _colors.lightBlue500,
    primary4Color: _colors.indigo500
  }
});

router.get('*', function (req, res, next) {
  (0, _reactRouter.match)({ routes: _routes2.default, location: location }, function (err, redirectLocation, renderProps) {
    if (err) {
      console.error(err);
      return res.status(500).end('服务端错误！');
    }

    var appHtml = ReactDOMServer.renderToString(
      _react2.default.createElement(
        _MuiThemeProvider2.default, 
        { muiTheme: muiTheme }, 
        _react2.default.createElement(
          _reactRedux.Provider, 
          { store: store }, 
          _react2.default.createElement(
            _reactRouter.RouterContext, 
            renderProps
          )
        )
      )
    );

    res.send((0, _indexHTML2.default)(appHtml));
  });
});

module.exports = router;